<?php

$current_year = date('Y');
$unix_timestamp = time();
$max_year = $current_year;

echo $javascript->link('work_experiences');

?>

<script>
    
    var get_districts_list_url = "<?php echo $this->Html->url(array(
                                                            'controller' => 'WorkExperiences',
                                                            'action'    => 'get_districts_list'
                                                        )); ?>"
    var get_towns_list_url = "<?php echo $this->Html->url(array(
                                                            'controller' => 'WorkExperiences',
                                                            'action'    => 'get_towns_list'
                                                        )); ?>"
    
    $(function() {
        
       init_dialog_button('add_company', 'company_form');
       init_dialog_button('add_position', 'position_form');
       init_dialog_button('add_sector', 'sector_form');
       
       company_form_dialog("<?php echo $this->Html->url(array(
                                                            'controller' => 'WorkExperiences',
                                                            'action'    => 'add_company'
                                                        )); ?>");
    
       position_form_dialog("<?php echo $this->Html->url(array(
                                                            'controller' => 'WorkExperiences',
                                                            'action'    => 'add_position'
                                                        )); ?>");
                                                                
       sector_form_dialog("<?php echo $this->Html->url(array(
                                                            'controller' => 'WorkExperiences',
                                                            'action'    => 'add_sector'
                                                        )); ?>");                                                         
    

       // ogni volta che scelgo una regione aggiorno la select che contiene le province
        
        $( '#company_region_id' ).change(function() {
            
            var region_id = $(this).val();
            if(region_id != '') {
                remove_all_options('company_district_id', 'Selezionare la provincia');
                remove_all_options('company_town_id', 'Selezionare la città');
                get_districts_list(region_id, get_districts_list_url, 'company_district_id');
            }
                
            
        })
        
        // ogni volta che scelgo una provincia aggiorno la select che contiene le città
        $( '#company_district_id' ).change(function() {
            
            var district_id = $(this).val();
            if(district_id != '') {
                remove_all_options('company_town_id', 'Selezionare la città');
                get_towns_list(district_id, get_towns_list_url, 'company_town_id');
            }
            
        })                                                 



    });
    


</script>

<!-- Add company form dialog -->
<div id="company_form" title="Aggiungi un'organizzazione/ente">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>

    <fieldset>
            <label for="company_name">Nome</label>
            <input type="text" name="company_name" id="company_name" class="text ui-widget-content ui-corner-all" />
            <label for="company_address">Indirizzo</label>
            <input type="text" name="company_address" id="company_address" class="text ui-widget-content ui-corner-all" />
            <label for="company_postalCode">CAP</label>
            <input type="text" name="company_level" id="company_level" class="text ui-widget-content ui-corner-all" />
            <label for="company_region_id">Regione</label>
            <select name="company_region_id" id="company_region_id" class="ui-widget-content ui-corner-all">
                <option value="">Selezionare la regione</option>
                <?php 
                    foreach($regions as $id => $region) { ?>
                        <option value="<?php echo $id ?>"><?php echo $region ?></option>
                <?php
                    }
                ?>
            </select>
            <label for="company_district_id">Provincia</label>
            <select type="text" name="company_district_id" id="company_district_id" class="ui-widget-content ui-corner-all" >
                <option value="">Selezionare la provincia</option>
            </select>
            <label for="company_town_id">Città</label>
            <select type="text" name="company_town_id" id="company_town_id" class="ui-widget-content ui-corner-all">
                <option value="">Selezionare la città</option>
            </select>
    </fieldset>

</div>
<!-- Add position form dialog -->
<div id="position_form" title="Aggiungi Posizione">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>
    
    <form>
    <fieldset>
            <label for="position_name">Nome</label>
            <input type="text" name="position_name" id="position_name" class="text ui-widget-content ui-corner-all" />
    </fieldset>
    </form>

</div>

<div id="sector_form" title="Aggiungi Settore">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>
    
    <form>
    <fieldset>
            <label for="sector_name">Nome</label>
            <input type="text" name="sector_name" id="sector_name" class="text ui-widget-content ui-corner-all" />
            <label for="sector_description">Descrizione</label>
            <input type="text" name="sector_description" id="sector_description" class="text ui-widget-content ui-corner-all" />
    </fieldset>
    </form>

</div>

<!-- fine HTML dialog -->

<?php

echo $form->create('WorkExperience');

?>

<fieldset>
    <legend>
        Nuova esperienza di lavoro
    </legend>

<?php

    echo $form->input('from', array('type'=>'date', 
                                    'selected'=>$unix_timestamp,
                                    'minYear'=>1920, 
                                    'maxYear'=>$max_year, 
                                    'label' => 'Da'));
    echo $form->input('to', array('type'=>'date', 
                                  'selected'=>$unix_timestamp,
                                  'minYear'=>1920, 
                                  'maxYear'=>$max_year, 
                                  'label' => 'A'));
    echo $form->input('activity', array('label' => 'Attivit&agrave;',
                                      'rows' => '3'));
    echo $form->input('company_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $companies,
                                 'label' => 'Azienda/Ente'));  
    echo $html->link('Aggiungi Azienda/Ente', '#', array('onclick'=>'return false;', 'id'=>'add_company', 'class'=>''));

    echo $form->input('position_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $positions,
                                 'label' => 'Posizione'));   
    echo $html->link('Aggiungi Posizione', '#', array('onclick'=>'return false;', 'id'=>'add_position', 'class'=>''));
    
    echo $form->input('sector_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $sectors,
                                 'label' => 'Settore'));    
    echo $html->link('Aggiungi Settore', '#', array('onclick'=>'return false;', 'id'=>'add_sector', 'class'=>''));
    
    echo $form->input('person_id', array(
                                    'type' => 'hidden',
                                    'value' => $person_id));

?>
    
</fieldset>
    
<?php    
    
    echo $form->end('Salva');

?>